package com.zzyl.nursing.job;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.service.IContractService;
import com.zzyl.nursing.service.impl.ContractServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;


@Component("UpdateContractStatusJob")
public class UpdateContractStatusJob {
    @Autowired
    private IContractService contractService;

    public void statusJob(){
        System.out.println("入住合同状态定时任务");
//         状态 (0: 未生效, 1: 已生效, 2: 已过期, 3: 已失效)
//        List<Contract> list = contractService.list();
//        for (Contract contract : list) {
//            LocalDateTime startDate = contract.getStartDate();
//            LocalDateTime endDate = contract.getEndDate();
//            if (startDate.isAfter(LocalDateTime.now())){
//                contract.setStatus(0);
//            }else if (startDate.isBefore(LocalDateTime.now()) && endDate.isAfter(LocalDateTime.now())){
//                contract.setStatus(1);
//            }else if (endDate.isBefore(LocalDateTime.now())){
//                contract.setStatus(2);
//            }
//            contractService.updateContract(contract);
//        }
        LambdaUpdateWrapper<Contract> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Contract::getStatus, 1);
        wrapper.eq(Contract::getStatus, 0);
        wrapper.lt(Contract::getStartDate, LocalDateTime.now());
        wrapper.gt(Contract::getEndDate, LocalDateTime.now());
        contractService.update(wrapper);
        LambdaUpdateWrapper<Contract> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Contract::getStatus, 2);
        updateWrapper.eq(Contract::getStatus, 1);
        updateWrapper.lt(Contract::getEndDate, LocalDateTime.now());
        contractService.update(updateWrapper);


    }
}
